﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBupro.Models;
using iBuproModel.Repositories.Interfaces;
using IBupro.Controllers.Classes;
using iBuproModel;

namespace IBupro.Controllers
{
    public class SearchController : CustomController
    {
        public ActionResult Index()
        {
            return RedirectToAction("DiplomProjects");
        }

        public ActionResult DiplomProjects()
        {
          

            iBuproModel.User user = users.GetUserByUserName(User.Identity.Name);

            DiplomProjectSearchModel model = new DiplomProjectSearchModel();
            if (user != null)
            {
                if (user.StudentField != null)
                    model.DepartmentId = departments.First(p => p.Id == p.Id).Id;
            }

            model.ProjectList = diplomProjects.GetAll().OrderBy(p => p.Created);

            return View(model);
        }

        [HttpPost]
        public ActionResult DiplomProjects(DiplomProjectSearchModel search)
        {
           
           
            IEnumerable<DiplomProject> projects = diplomProjects.GetAll().ToList();
            projects = projects.Where(p => p.DiplomProjectType.Id == search.ProjectTypeId && p.Department.Id == search.DepartmentId);

            if (search.FreeOnly)
                projects = projects.Where(p => p.DiplomProjectState.Id == 1);

            if (!string.IsNullOrEmpty(search.SearchString))
            {
                char[] separators = new char[] { ' ', ',' };

                string[] tagList = search.SearchString.Split(separators, StringSplitOptions.RemoveEmptyEntries);


                var results = new List<DiplomProject>();
                foreach (string tag in tagList)
                {
                    List<DiplomProject> proj = projects.Where(p => p.Tags.Where(t => t.Name.ToLower().Contains(tag.ToLower())).FirstOrDefault() != null || p.Name.ToLower().Contains(tag) || p.Detail.ToLower().Contains(tag)).ToList();
                    foreach (DiplomProject i in proj)
                    {
                        if (!results.Contains(i))
                            results.Add(i);
                    }
                    
                }
                search.ProjectList = results;
            }
            else
            {
                search.ProjectList = projects;
            }
            

            return View(search);
        }

        public ActionResult StudentProjects()
        {
          
            IProjectRepository studentProjects = repositoryService.ProjectRepositoryCreateInstance();
            IStudentFieldRepository studentFields = repositoryService.StudentFieldRepositoryCreateInstance();

            iBuproModel.User user = users.GetUserByUserName(User.Identity.Name);

            StudentProjectSearchModel model = new StudentProjectSearchModel();
            if (user != null)
            {
            }
            else
            {
                model.StudentFieldId = studentFields.First(p => p != null).Id;
            }

            if (model.SubjectId == 0)
                model.ProjectList = studentProjects.GetAll().OrderBy(p => p.Created);
            else
                model.ProjectList = studentProjects.GetAll(p => p.Subject.Id == model.SubjectId).OrderBy(p => p.Created);

            return View(model);
        }

        [HttpPost]
        public ActionResult StudentProjects(StudentProjectSearchModel search)
        {
            IProjectRepository studentProjects = repositoryService.ProjectRepositoryCreateInstance();
            IEnumerable<Project> projects = studentProjects.GetAll().ToList();

            if (search.SubjectId != 0)
                projects = projects.Where(p => p.Subject.Id == search.SubjectId);
                


            if (search.FreeOnly)
                projects = projects.Where(p => p.MemberCount > p.Users.Count);

            if (!string.IsNullOrEmpty(search.SearchString))
            {
                char[] separators = new char[] { ' ', ',' };

                string[] tagList = search.SearchString.Split(separators, StringSplitOptions.RemoveEmptyEntries);


                var results = new List<Project>();
                foreach (string tag in tagList)
                {
                    List<Project> proj = projects.Where(p => p.Tags.Where(t => t.Name.ToLower().Contains(tag.ToLower())).FirstOrDefault() != null || p.Name.ToLower().Contains(tag) || p.Detail.ToLower().Contains(tag)).ToList();
                    foreach (Project i in proj)
                    {
                        if (!results.Contains(i))
                            results.Add(i);
                    }

                }
                search.ProjectList = results;
            }
            else
            {
                search.ProjectList = projects;
            }


            return View(search);
        }

    }
}
